<?php




	include_once 'json_function.php';

	
	$ini_array = parse_ini_file("config/config.ini");
	
	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	$clinicNo = mysqli_real_escape_string($conn, $_POST["clinicNo"]);
	$clinicName = mysqli_real_escape_string($conn, $_POST["clinicName"]);
	$address = mysqli_real_escape_string($conn, $_POST["clinicAddress"]);
	$detail = mysqli_real_escape_string($conn, $_POST["detail"]);
	$operateType = 0;
	
	$operateSql = "";
	if( $clinicNo == 0 ){
		$operateSql = "insert into clinic( Clinic_name, Clinic_address, Clinic_detail)values('".$clinicName."','".$address."','".$detail."')";
		
	}else{
		$getCountSql = "select count(*) count from Clinic where Clinic_no=".$clinicNo;
		$countResult = mysqli_query($conn,$getCountSql);
		$row = mysqli_fetch_array($countResult);
		$count = $row["count"];
		if( $count == 0 ){
			$operateSql = "insert into clinic( Clinic_name, Clinic_address, Clinic_detail)values('".$clinicName."','".$address."','".$detail."')";
			$operateType = 1;
		}else{
			$operateSql = "update clinic set Clinic_name='".$clinicName."', Clinic_address='".$address."', Clinic_detail='".$detail."' where Clinic_no=".$clinicNo;
			$operateType = 2;
		}
		
	}
	
	
	$result = mysqli_query($conn, $operateSql);
	
	if( $clinicNo == 0 ){
		$clinicNo = mysqli_insert_id($conn);
	}
	
	if( $result ){
		
		echo JsonCharFilter("{\"data\":{\"status\":\"success\", \"clinicNo\":\"".$clinicNo."\",\"clinicName\":\"".replaceQuoteAndSlash($clinicName)."\"}}");
	}else{
		
		echo JsonCharFilter("{\"data\":{\"status\":\"fail\", \"msg\":\"".mysqli_error($conn)."\"}}");
	}
	
?>